home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
6_11.lha
/
6_11
/
6_11_neg.c
< prev
next >
Wrap
Text File
|
1993-08-08
|
1KB
|
67 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
*
Negate u[1..n] to form w[0..n]
Uses a variation on:
The Art of Computer Programming, volume 2
D. Knuth, Section 4.3.1, Algorithm A
/
include <arbint.h>
include <debug.h> // DELETE
rbint operator-(const arbint& u)
int ulen = u.p->length;
int wlen = ulen + u.isneg();
ARB_type *wv = new ARB_type[wlen];
ARB_type *uv = u.p->value;
bug&4) outputarb(cerr, "\n\nneg:\nu=", uv, ulen); // DELETE
/*
A1 [Initialize]
set j <- n
k <- 0
becomes
k <- 1
A3(a) [Loop on j]
decrease j by one
*/
ARB_Ltype k = 1;
for (int uj = ulen - 1, wj = wlen - 1;
uj >= 0; uj--, wj--)
{
/*
A2(a) [Add digits]
set w[j] <- (u[j] + v[j] + k) mod b
becomes
set w[j] <- (~u[j] + k) mod b
*/
ARB_type l1 = ~uv[uj];
ARB_Ltype l = l1 + k;
wv[wj] = l; // % ARB_based
/*
A2(b)
k <- (u[j] + v[j] + k) / b
*/
k = (l / ARB_base ) ? 1 : 0;
}
/*
A3(b)
Set w[0] <- ~0 + k
*/
if (wj == 0)
{ if (debug&4) cerr << "wj==0, k=" << k << "\n"; // DELETE
wv[0] = k ? ~0 : 0;
} // DELETE
/* Normalize and return */
f (debug&4) outputarb(cerr, "wv=", wv, wlen); // DELETEd arbint w(wv, wlen);
f (debug&4) outputarb(cerr, "w=", w.p->value, w.p->length); // DELETE
return w;